package phoenix.web.test_objects.adsbridge;

import java.util.Set;

import org.openqa.selenium.Cookie;
import org.testng.TestException;

import phoenix.core.PhoenixManager;
import phoenix.core.base.TestSuiteBase;
import phoenix.core.base.extensions.TestBaseWebExt;
import phoenix.core.data_models.UserProfileDataModel.PaymentDetails;
import phoenix.web.locators.adsbridge.AdsBridgeLocators;
import phoenix.web.test_objects.PaymentTests;
import phoenix.web.test_objects.data_providers.adsbridge.AdsBridgeDataProviders;

/**
 * Class contains test methods for AdsBridge
 */
public class AdsBridgeTests extends TestBaseWebExt
{
	String p_fileName = "report.csv";
	public PaymentTests paymentTests = new PaymentTests(manager);

	public AdsBridgeTests(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	public AdsBridgeLocators elements()
	{
		return new phoenix.web.locators.adsbridge.AdsBridgeLocators();
	}

	public AdsBridgeDataProviders dataProviders = new AdsBridgeDataProviders(manager);

	/**
	 * Precondition test
	 */
	public void preconditionTest()
	{
		try
		{
			this.getCookies(testModel().getSiteURL());
			this.siteLogout(testModel().getSiteURL());
		}
		catch (Exception p_ex)
		{
			this.addCSVReport();
			log().errorAndScreen("preconditionTest: " + p_ex);
		}
		finally
		{
			test().forErrors();
		}
	}

	/**
	 * Rule execution test
	 * @param p_jump
	 * @param p_site
	 * @param p_page
	 */
	public void ruleExecutionTest(String p_jump, String p_site, String p_page, PaymentDetails p_details)
	{
		try
		{
			this.passJump(p_jump);

			String p_url = wd().getCurrentUrl();
			this.writeToCSV("Generated url: ", p_url);

			if (!this.isExpectedSite(p_site))
			{
				throw new TestException("Expected site not displayed");
			}
			else if (!this.isExpectedPage(p_page))
			{
				throw new TestException("Expected page not displayed");
			}
			else
			{
				this.clickOneClickSubmit();

				if (page().paymentPage().isPaymentAuthorizationPresent())
				{
					paymentTests.paymentAuthorizationTest(p_details);
				}

				if (!this.isOnSite(p_site))
				{
					throw new TestException("Co-registration failed: not logged in: [" + p_site + "]");
				}
				else
				{
					this.getCookies(p_site);
					this.siteLogout(p_site);
				}
			}
		}
		catch (Exception p_ex)
		{
			this.addCSVReport();
			log().errorAndScreen("ruleExecutionTest: " + p_ex);
		}
		finally
		{
			test().forErrors();
		}
	}

	/**
	 * Expected page test
	 * @param p_jump
	 * @param p_site
	 * @param p_page
	 */
	public void expectedPageTest(String p_jump, String p_site, String p_page, boolean p_redirect)
	{
		try
		{
			if (p_redirect)
			{
				this.passJump(p_jump);
			}

			String p_url = wd().getCurrentUrl();
			this.writeToCSV("Generated url: ", p_url);

			if (!this.isExpectedSite(p_site))
			{
				throw new TestException("Expected site not displayed");
			}
			else if (!this.isExpectedPage(p_page))
			{
				throw new TestException("Expected page not displayed");
			}
		}
		catch (Exception p_ex)
		{
			this.addCSVReport();
			log().errorAndScreen("expectedPageTest: " + p_ex);
		}
		finally
		{
			test().forErrors();
		}
	}

	/**
	 * Set cookies test
	 * @param p_site
	 */
	public void setCookiesTest(String p_site)
	{
		try
		{
			wd().openURL(p_site);
			wd().setCookie("ip_address", "69.147.251.50", p_site, "/",
					manager.cronos().convertStringToDate("2020-01-18 00:00:00.0", "yyyy-MM-dd HH:mm:ss.S"));
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("setCookiesTest: " + p_ex);
		}
		finally
		{
			test().forErrors();
		}
	}

	public void getCookies(String p_site)
	{
		wd().openURL("www.ab4tn.com");
		Set<Cookie> cookies = wd().driver.manage().getCookies();

		for (Cookie cookie : cookies)
		{
			writeToCSV("Cookies data: [" + p_site + "]", String.valueOf(cookie));
		}
	}

	public void writeToCSV(String p_name, String p_value)
	{
		String p_filePath = System.getProperty("user.dir")
				+ "\\reports"
				+ "\\" + TestSuiteBase.TEST_SUITE_NAME
				+ "\\" + TestSuiteBase.TEST_SUITE_DATE
				+ "\\" + TestSuiteBase.TEST_SUITE_TIME
				+ "\\" + p_fileName;

		String[] logData = new String[2];
		logData[0] = p_name;
		logData[1] = p_value;

		manager.csv(p_filePath).write(logData);
	}

	public void passJump(String p_jump)
	{
		wd().openURL(p_jump);
		wd().wait(5);

		if (wd().isElementPresent(elements().btnAgreeArea))
		{
			wd().clickByJS(elements().btnAgreeArea);
		}
		else if (wd().isElementPresent(elements().btnAgreeImg))
		{
			wd().clickByJS(elements().btnAgreeImg);
		}
		else if (wd().isElementPresent(elements().btnStepsAgree))
		{
			wd().clickByJS(elements().btnStepsAgree);
		}
		else
		{
			log().errorAndScreen("Cannot pass jump, submit button undefined");
		}
	}

	public boolean isExpectedSite(String p_site)
	{
		return wd().isUrlContains(p_site, false);
	}

	public boolean isExpectedPage(String p_page)
	{
		return wd().isUrlContains(p_page, false);
	}

	public void clickOneClickSubmit()
	{
		wd().waitForPresence(elements().btnSubmitOneClick, "One click button 'Sumbit'");
		wd().wait(5);
		wd().click(elements().btnSubmitOneClick);
		wd().wait(5);
	}

	public void siteLogout(String p_site)
	{
		wd().openURL(p_site + "/site/logout");
		wd().waitForPresence(page().indexPage().elements().blockRegForm, "Block 'Registration form'");
	}

	public boolean isOnSite(String p_site)
	{
		return wd().isUrlContains(p_site + "/search", false);
	}

	public void addCSVReport()
	{
		String p_filePath = System.getProperty("user.dir")
				+ "\\reports"
				+ "\\" + TestSuiteBase.TEST_SUITE_NAME
				+ "\\" + TestSuiteBase.TEST_SUITE_DATE
				+ "\\" + TestSuiteBase.TEST_SUITE_TIME
				+ "\\" + p_fileName;

		manager.redmine().addAttachment(p_filePath, p_fileName, "text/csv");
	}
}
